/*************************************************************************
 * This file is part of HeroesForge.
 *
 * HeroesForge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HeroesForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HeroesForge.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
package org.diskordia.heroesforge.console;

import javax.swing.JPanel;
import java.awt.Frame;
import javax.swing.JDialog;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;

import java.awt.SystemColor;
import java.awt.Font;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

import org.diskordia.heroesforge.client.presentation.ICommand;
import org.diskordia.heroesforge.console.utils.GuiUtils;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

/**
 * @author FSuite
 *
 */
public class ConnectionDialog extends JDialog implements IConnectionDialogView{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabelServer = null;
	private JTextField jTextFieldServerUrl = null;
	private JPanel jPanelAuthentication = null;
	private JLabel jLabelUserId = null;
	private JLabel jLabelPassword = null;
	private JTextField jTextFieldUserName = null;
	private JPasswordField jPasswordField = null;
	private JCheckBox jCheckBoxAutoConnect = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonConnect = null;
	private JButton jButtonCancel = null;
	private JPanel jPanelEmptySpace = null;
	
	private ICommand commandConnect;
	private ICommand commandCancel;  //  @jve:decl-index=0:
	
	private ResourceMap resourceMap = Application.getInstance(HeroesForge.class).getContext().getResourceMap(ConnectionDialog.class);
	
	/**
	 * @param owner
	 */
	@Inject
	public ConnectionDialog(@Named("MainView") Frame owner) {
		super(owner);
		initialize();
		
		GuiUtils.centerWithinParent(this);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(354, 233);
		this.setResizable(false);
		this.setModal(true);
		this.setTitle(this.resourceMap.getString("title"));
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.weighty = 2.0;
			gridBagConstraints8.fill = GridBagConstraints.BOTH;
			gridBagConstraints8.anchor = GridBagConstraints.EAST;
			gridBagConstraints8.gridy = 3;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.anchor = GridBagConstraints.CENTER;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.insets = new Insets(3, 5, 3, 5);
			gridBagConstraints2.gridwidth = 1;
			gridBagConstraints2.gridy = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new Insets(3, 5, 3, 5);
			gridBagConstraints1.ipadx = 0;
			gridBagConstraints1.ipady = 0;
			gridBagConstraints1.gridwidth = 1;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(3, 5, 3, 5);
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.gridy = 0;
			jLabelServer = new JLabel();
			jLabelServer.setText(this.resourceMap.getString("jLabelServer.text"));
			jLabelServer.setToolTipText("");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(jLabelServer, gridBagConstraints);
			jContentPane.add(getJTextFieldServerUrl(), gridBagConstraints1);
			jContentPane.add(getJPanelAuthentication(), gridBagConstraints2);
			jContentPane.add(getJPanelButtons(), gridBagConstraints8);
		}
		return jContentPane;
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IView#CloseView()
	 */
	@Override
	public void CloseView() {
		this.setVisible(false);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IView#ShowView()
	 */
	@Override
	public void ShowView() {
		this.setVisible(true);		
	}

	/**
	 * This method initializes jTextFieldServerUrl	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldServerUrl() {
		if (jTextFieldServerUrl == null) {
			jTextFieldServerUrl = new JTextField();
		}
		return jTextFieldServerUrl;
	}

	/**
	 * This method initializes jPanelAuthentication	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelAuthentication() {
		if (jPanelAuthentication == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints7.gridy = 2;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridy = 1;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridy = 1;
			jLabelPassword = new JLabel();
			jLabelPassword.setText(this.resourceMap.getString("jLabelPassword.text"));
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.gridy = 0;
			jLabelUserId = new JLabel();
			jLabelUserId.setText(this.resourceMap.getString("jLabelUserId.text"));
			jPanelAuthentication = new JPanel();
			jPanelAuthentication.setLayout(new GridBagLayout());
			jPanelAuthentication.setBorder(BorderFactory.createTitledBorder(null, this.resourceMap.getString("jPanelAuthentication.border.text"), TitledBorder.LEFT, TitledBorder.TOP, new Font("Dialog", Font.BOLD, 12), SystemColor.inactiveCaption));
			jPanelAuthentication.add(jLabelUserId, gridBagConstraints3);
			jPanelAuthentication.add(jLabelPassword, gridBagConstraints4);
			jPanelAuthentication.add(getJTextFieldUserName(), gridBagConstraints5);
			jPanelAuthentication.add(getJPasswordField(), gridBagConstraints6);
			jPanelAuthentication.add(getJCheckBoxAutoConnect(), gridBagConstraints7);
		}
		return jPanelAuthentication;
	}

	/**
	 * This method initializes jTextFieldUserName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldUserName() {
		if (jTextFieldUserName == null) {
			jTextFieldUserName = new JTextField();
		}
		return jTextFieldUserName;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jCheckBoxAutoConnect	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxAutoConnect() {
		if (jCheckBoxAutoConnect == null) {
			jCheckBoxAutoConnect = new JCheckBox();
			jCheckBoxAutoConnect.setText(this.resourceMap.getString("jCheckBoxAutoConnect.text"));
		}
		return jCheckBoxAutoConnect;
	}

	/**
	 * This method initializes jPanelButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.gridy = 0;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 2;
			gridBagConstraints10.ipadx = 10;
			gridBagConstraints10.ipady = 0;
			gridBagConstraints10.insets = new Insets(3, 3, 3, 5);
			gridBagConstraints10.anchor = GridBagConstraints.CENTER;
			gridBagConstraints10.weightx = 0.0;
			gridBagConstraints10.gridy = 0;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 1;
			gridBagConstraints9.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints9.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints9.gridwidth = 1;
			gridBagConstraints9.ipadx = 10;
			gridBagConstraints9.ipady = 0;
			gridBagConstraints9.weightx = 0.0;
			gridBagConstraints9.gridy = 0;
			jPanelButtons = new JPanel();
			jPanelButtons.setLayout(new GridBagLayout());
			jPanelButtons.add(getJButtonConnect(), gridBagConstraints9);
			jPanelButtons.add(getJButtonCancel(), gridBagConstraints10);
			jPanelButtons.add(getJPanelEmptySpace(), gridBagConstraints11);
		}
		return jPanelButtons;
	}

	/**
	 * This method initializes jButtonConnect	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonConnect() {
		if (jButtonConnect == null) {
			jButtonConnect = new JButton();
			jButtonConnect.setText(this.resourceMap.getString("jButtonConnect.text"));
			jButtonConnect.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				ICommand command = ConnectionDialog.this.getCommandConnect();
					
					if(command != null) {
						command.Execute();
					}
				}
			});
		}
		return jButtonConnect;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText(this.resourceMap.getString("jButtonCancel.text"));
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ICommand command = ConnectionDialog.this.getCommandCancel();
					
					if(command != null) {
						command.Execute();
					}
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jPanelEmptySpace	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelEmptySpace() {
		if (jPanelEmptySpace == null) {
			jPanelEmptySpace = new JPanel();
			jPanelEmptySpace.setLayout(new GridBagLayout());
		}
		return jPanelEmptySpace;
	}

	/**
	 * @param commandConnect the commandConnect to set
	 */
	public void setCommandConnect(ICommand commandConnect) {
		this.commandConnect = commandConnect;
	}

	/**
	 * @return the commandConnect
	 */
	public ICommand getCommandConnect() {
		return commandConnect;
	}

	/**
	 * @param commandCancel the commandCancel to set
	 */
	public void setCommandCancel(ICommand commandCancel) {
		this.commandCancel = commandCancel;
	}

	/**
	 * @return the commandCancel
	 */
	public ICommand getCommandCancel() {
		return commandCancel;
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#getPassword()
	 */
	@Override
	public String getPassword() {
		return new String(this.jPasswordField.getPassword());
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#getServerUrl()
	 */
	@Override
	public String getServerUrl() {
		return this.jTextFieldServerUrl.getText();
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#getUserName()
	 */
	@Override
	public String getUserName() {
		return this.jTextFieldUserName.getText();
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#isAutoConnect()
	 */
	@Override
	public boolean isAutoConnect() {
		return this.jCheckBoxAutoConnect.isSelected();
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#setAutoConnect(boolean)
	 */
	@Override
	public void setAutoConnect(boolean enabled) {
		this.jCheckBoxAutoConnect.setSelected(enabled);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#setPassword(java.lang.String)
	 */
	@Override
	public void setPassword(String password) {
		if(password == null){
			throw new IllegalArgumentException("The password string is a null reference.");
		}
		
		this.jPasswordField.setText(password);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#setServerUrl(java.lang.String)
	 */
	@Override
	public void setServerUrl(String url) {
		if(url == null){
			throw new IllegalArgumentException("The url string is a null reference.");
		}
		
		this.jTextFieldServerUrl.setText(url);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.presentation.IConnectionDialogView#setUserName(java.lang.String)
	 */
	@Override
	public void setUserName(String user) {
		if(user == null){
			throw new IllegalArgumentException("The user string is a null reference.");
		}
		
		this.jTextFieldUserName.setText(user);
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
